package com.shisan.tingshu.search.api;

import com.shisan.tingshu.common.constant.RedisConstant;
import com.shisan.tingshu.common.result.Result;
import com.shisan.tingshu.query.search.AlbumIndexQuery;
import com.shisan.tingshu.search.service.ItemService;
import com.shisan.tingshu.search.service.SearchService;
import com.shisan.tingshu.vo.search.AlbumInfoIndexVo;
import com.shisan.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.shisan.cache.aspect.annotaion.Cacheable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Tag(name = "专辑详情管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemApiController {

	@Autowired
	private ItemService itemService;

	@Autowired
	private SearchService searchService;

	@PostMapping("/albumOnSale/{albumId}")
	@Operation(summary = "专辑上架")
	public Result albumOnSale(@PathVariable(value = "albumId") Long albumId){

		itemService.albumOnSale(albumId);
		return Result.ok();
	}

	@DeleteMapping("/albumOffSale/{albumId}")
	@Operation(summary = "专辑下架")
	public Result albumOffSale(@PathVariable(value = "albumId") Long albumId) {
		itemService.albumOffSale(albumId);
		return Result.ok();
	}

	// TODO：此处应该是定时任务，定时将数据库中的专辑信息同步到es中。此处将代码写死
	@PostMapping("/batchAlbumOnSale")
	@Operation(summary = "专辑批量上架")
	public Result batchAlbumOnSale(){
		for (int i = 1; i <= 1577; i++) {
			itemService.albumOnSale((long) i);
		}
		return Result.ok();
	}

	@DeleteMapping("/batchAlbumOffSale")
	@Operation(summary = "专辑批量下架")
	public Result batchAlbumOffSale() {
		itemService.batchAlbumOffSale();
		return Result.ok();
	}

	@GetMapping("/channel/{c1Id}")
	@Operation(summary = "频道页数据展示")
	public Result channel(@PathVariable(value = "c1Id") Long c1Id) {

		// 前端需要的是一个由多个Map组成的集合
		// k：baseCategory，即三级分类对象
		// v：list，该三级分类下热度值最高的6张专辑
		List<Map<String, Object>> mapList = searchService.channel(c1Id);
		return Result.ok(mapList);
	}

	@PostMapping
	@Operation(summary = "带条件的搜索")
	public Result search(@RequestBody AlbumIndexQuery albumIndexQuery) {
		AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
		return Result.ok(albumSearchResponseVo);
	}

	@GetMapping("/completeSuggest/{input}")
	@Operation(summary = "搜索关键字自动补全")
	public Result completeSuggest(@PathVariable(value = "input") String input) {
		Set<String> set = searchService.completeSuggest(input);
		return Result.ok(set);
	}

	@GetMapping("/{albumId}")
	@Operation(summary = "根据专辑id查询专辑详情")
	@Cacheable(cacheKey = RedisConstant.CACHE_INFO_PREFIX + "#{#args[0]}", // 要传递方法中的第几个参数就写第几个参数
			lockKey = RedisConstant.ALBUM_LOCK_SUFFIX+"#{#args[0]}",
			bloomKey = "#{#args[0]}",
			enableBloomFilter = true,
			enableLock = true)
	public Result getAlbumInfo(@PathVariable(value = "albumId") Long albumId) {
		Map<String, Object> result = itemService.getAlbumInfo(albumId);
		return Result.ok(result);
	}

	@PostMapping("/preRankingToCache")
	@Operation(summary = "将ES中的排行榜数据缓存到Redis中")
	public Result preRankingToCache() {
		itemService.preRankingToCache();
		return Result.ok();
	}

	@GetMapping("/findRankingList/{c1Id}/{dimension}")
	@Operation(summary = "查询排行榜")
	public Result findRankingList(@PathVariable(value = "c1Id") Long c1Id,
								  @PathVariable(value = "dimension") String dimension) {
		List<AlbumInfoIndexVo> albumInfoIndexVoList = itemService.findRankingList(c1Id, dimension);
		return Result.ok(albumInfoIndexVoList);
	}
}

